const _ = require('lodash');
const config = require('../config');
const fse = require('fs-extra');
const JsonIO = require('./utils/jsonio');
const Renderer = require('./utils/renderer');

const adocInfoIO = new JsonIO(config.dbPath);
const adocRenderer = new Renderer(`${config.templateDir}/index.hbs`);

module.exports = async () => {
  const adocInfo = adocInfoIO.read();
  console.log(JSON.stringify(adocInfo, null, '    '));
  const orderedAdocList = _(adocInfo).values().orderBy('date', 'desc').value();
  const html = adocRenderer.render({
    adocList: orderedAdocList,
    title: config.site.title,
    nav: config.site.nav,
    logo: config.site.logo,
    analysis: config.site.analysis
  });
  await fse.outputFile(config.deployDir + '/index.html', html);
};
